package interfaces.spawnMenu;

import main.InitGame;
import main.listeners.listenerClasses.LanguageListener;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.layout.RowLayout;
import org.fenggui.util.Color;

import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.SpawnMenuText;

import interfaces.GUISource;
import interfaces.superWidgets.InterfaceContainer;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.MoneyListener;

public class MoneyWindow extends InterfaceContainer implements MoneyListener {
	
	private static final String BORDER = " ";
	
	private InterfaceLabel moneyTextLabel, moneyLabel;
	
	public MoneyWindow(AbsSpawnMenu spawnMenu) {
		super(new RowLayout(true));
		
		InitGame.get().addLanguageListener(new LanguageListener() {
			@Override
			public void languageChanged() {
				changeLanguage();
			}
		});
		
		ImageFont font = GUISource.bigFont;
		Color color = GUISource.green;
		setSize(spawnMenu.getWidth() / 30, font.getHeight() / 3 * 4);
		
		moneyTextLabel = new InterfaceLabel(font);
		GUISource.setTextColor(moneyTextLabel, color);
		addWidget(moneyTextLabel);
		
		moneyLabel = new InterfaceLabel(font);
		GUISource.setTextColor(moneyLabel, color);
		addWidget(moneyLabel);
		
		changeLanguage();

		int initMoney = spawnMenu.getGame().getCurrentPlayer().getMoney();
		updateMoney(initMoney);
	}
	
	protected void changeLanguage() {
		String text = LanguageLoader.get(SpawnMenuText.Money) + ":";
		moneyTextLabel.setText(text);
		updateWidth();
	}
	
	protected void updateWidth() {
		layout();
		setWidth(moneyTextLabel.getWidth() + moneyLabel.getWidth());
		layout();
	}
	
	@Override
	public void updateMoney(int newMoney) {
		moneyLabel.setText(BORDER + newMoney);
		updateWidth();
	}
}